package root.electronclass.electronclass.ext

import root.electronclass.electronclass.common.util.ToastUtil
import root.electronclass.electronclass.mvp.entity.ApiException
import root.electronclass.electronclass.App
import root.electronclass.electronclass.BuildConfig
import kotlin.coroutines.CoroutineContext

/**
 *
 * @author Lokiy
 * @date 2023/2/4 14:26
 */

fun CoroutineExceptionHandler(isShowError:Boolean = true, handler: (Throwable) -> Unit = {}) =
    kotlinx.coroutines.CoroutineExceptionHandler { _: CoroutineContext, throwable: Throwable ->
        if (BuildConfig.DEBUG) {
            throwable.printStackTrace()
        }
        if (throwable is ApiException && isShowError) {
            ToastUtil.showToast(App.app, throwable.message)
        }
        handler.invoke(throwable)
    }